import {defineStore} from "pinia";
import {IAuthority, IDept, ISidebar} from "@/iwb/IConfig";
import {
    addDepartment,
    addPhoneAuthority,
    deleteDepartment,
    deletePhoneAuthority,
    editDepartment,
    getPhoneAuthority, searchPhoneAuthority
} from "@/api/config";
import {IUser} from "@/iwb/IUser";
import {IParty} from "@/iwb/IParty";

export const useConfigStore = defineStore('useConfig', {
    state: () => ({
        sidebarMenus: [] as Array<ISidebar>,
        breadcrumb: [] as Array<string>,
        deptTree: [] as Array<IDept>,
        showPersonInfo: false,
        personInfo: {} as IUser,
        showUserDialog: true as boolean,
        showPartyNew: false,
        partyInfo: {} as IParty
    }),
    actions: {
        /**
         * 边框菜单
         * @param menu
         */
        addSidebarMenu(menu: ISidebar | Array<ISidebar>): void {
            if (menu instanceof Array<ISidebar>) {
                this.sidebarMenus.push(...menu);
            } else {
                this.sidebarMenus.push(menu);
            }
        },
        pushBreadcrumb(crumb: string): void {
            this.breadcrumb.push(crumb);
        },
        popBreadcrumb(currCrumb: string): void {
            let length = this.breadcrumb.length - 1;
            let index = this.breadcrumb.findIndex(item => item === currCrumb);
            if (index >= 0 && length > 0) {
                this.breadcrumb.splice(index + 1, length);
            }
        },
        resetBreadcrumb(): void {
            this.breadcrumb = [];
        },
        /**
         * 部门信息相关函数
         */
        addDepartment(data: IDept) {
            return new Promise((resolve, reject) => {
                addDepartment(data).then(res => {
                    resolve(res);
                }).catch(error => {
                   reject(error);
                })
            })
        },
        /**
         * 编辑部门信息
         * @param data
         */
        editDepartment(data: IDept) {
            return new Promise((resolve, reject) => {
                editDepartment(data).then(res => {
                    resolve(res);
                }).catch(error => {
                    reject(error);
                })
            })
        },
        /**
         * 删除部门信息
         * @param id
         */
        deleteDepartment(id: number) {
            return new Promise((resolve, reject) => {
                deleteDepartment(id).then(res => {
                    resolve(res);
                }).catch(error => {
                    reject(error);
                })
            })
        },
        /**
         * 添加授权码
         * @param data
         */
        addPhoneAuthority(data: IAuthority) {
            return new Promise(((resolve, reject) => {
                addPhoneAuthority(data).then(res=>{
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            }))
        },
        /**
         * 分页查询授权信息
         * @param pageIndex
         * @param pageSize
         */
        getPhoneAuthority(pageIndex: number, pageSize: number) {
            return new Promise((resolve, reject) => {
                getPhoneAuthority(pageIndex, pageSize).then(res=>{
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            })
        },
        /**
         * 删除授权信息
         * @param loginphone
         */
        deletePhoneAuthority(loginphone: string) {
          return new Promise((resolve, reject) => {
              deletePhoneAuthority(loginphone).then(res=>{
                  resolve(res);
              }).catch(error=>{
                  reject(error);
              })
          })
        },
        /**
         * 查询授权信息
         * @param loginphone
         */
        searchPhoneAuthority(loginphone: string) {
            return new Promise((resolve, reject) => {
                searchPhoneAuthority(loginphone).then(res=>{
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                })
            })
        },
        setHowUserDialog(show: boolean): void {
            this.showUserDialog = show;
        }
    }
})